package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.properties.BaiDuDiTuProperties;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 百度地图工具类
 */
@Component
public class BaiDuMapUtil {
    @Autowired
    private  BaiDuDiTuProperties baiDuDiTuProperties;

        /*https://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求 */
    //百度地图接口地址,获取经纬度坐标
    private static final String BAIDU_JINGWEI = "https://api.map.baidu.com/geocoding/v3";

     /* https://api.map.baidu.com/directionlite/v1/riding?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK   //GET请求*/
    //百度地图获距离
    private static final String BAIDU_DISTANCE="https://api.map.baidu.com/directionlite/v1/riding";

    /**
     * 获取地址的经纬度坐标
     * @return
     */
    public  String getLocation(String address){
        //调用微信接口服务，获得当前微信用户的openid
        Map<String, String> map = new HashMap<>();
        map.put("address",address);
        map.put("ak",baiDuDiTuProperties.getAk());
        map.put("output","json");


        //向微信发出doGet请求,并获取string类型数据
        String json = HttpClientUtil.doGet(BAIDU_JINGWEI, map);
        //将Json字符串转化为相应的对象
        JSONObject jsonObject = JSON.parseObject(json);
//        log.info("{}",jsonObject);
        String result = jsonObject.getString("result");
//        log.info("{}",result);

        JSONObject jsonObject1 = JSON.parseObject(result);
        String location = jsonObject1.getString("location");
//        log.info("{}",location);

        JSONObject jsonObject2 = JSON.parseObject(location);
        String lng = jsonObject2.getString("lng");
//        log.info("{}",lng);//经度值
        String lat = jsonObject2.getString("lat");
//        log.info("{}",lat);//纬度值
        String jw=lat+","+lng;
//        log.info("{}",jw);
        return jw;
    }


     /*
        https://api.map.baidu.com/directionlite/v1/riding?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK   //GET请求
        */

    /**
     * 百度地图获距离
     * @param shopAddress
     * @param AddressUser
     * @return
     */
    public Double getDiatance(String shopAddress,String AddressUser){

        //origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK
        Map<String, String> map = new HashMap<>();
        map.put("origin", shopAddress);
        map.put("destination",AddressUser );
        map.put("ak",baiDuDiTuProperties.getAk());

        //向微信发出doGet请求,并获取string类型数据
        String json = HttpClientUtil.doGet(BAIDU_DISTANCE, map);
        //将Json字符串转化为相应的对象
        JSONObject jsonObject = JSON.parseObject(json);
        String result = jsonObject.getString("result");
        int indexEnd = result.indexOf(",\"steps\"");
        int indexOfStart = result.indexOf("\"distance\":");
        String substring = result.substring(indexOfStart+11, indexEnd);
//        log.info("substring:{}",substring);
        double distance = Double.parseDouble(substring);
        return distance;
    }




}
